<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.name" clearable @change="handleFilter" class="filter-item" style="width: 200px;" placeholder="请输入产品名称" />
      <el-date-picker
        clearable
        class="filter-item"
        v-model="time"
        @change="changeTime"
        type="datetimerange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期">
      </el-date-picker>
      <el-button v-permission="['GET /admin/product/list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button v-permission="['POST /admin/product/create']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleSelectFormula">添加</el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-table :data="props.row.materialVOS" style="width: 100%">
            <el-table-column align="center" label="原料ID" prop="id" />
            <el-table-column align="center" label="原料名称" prop="name" />
            <el-table-column align="center" label="数量" prop="amount" />
            <el-table-column align="center" label="单位" prop="unit" />
            <el-table-column align="center" label="单价(元)" prop="price" />
            <el-table-column align="center" label="成本金额(元)" prop="totalPrice" />
          </el-table>
        </template>
      </el-table-column>
      <el-table-column align="center" label=产品方ID" prop="id" />

      <el-table-column align="center" label="产品名称" prop="name" />

      <el-table-column align="center" label="个数" prop="number" />

      <el-table-column align="center" label="成本金额(元)" prop="price" />

      <el-table-column align="center" label="简介" prop="brief" />

      <el-table-column align="center" label="创建时间" prop="addTime" />

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button v-permission="['POST /admin/formula/update']" type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>-->
          <el-button v-permission="['POST /admin/product/delete']" type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>

    <!-- 添加或修改对话框 -->
    <el-dialog title="选择配方" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="配方名称" prop="name">
          <el-select v-model="dataForm.formulaId" clearable filterable placeholder="请选择">
            <el-option
              v-for="item in formula"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="openCreate">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<style>
  .table-expand {
    font-size: 0;
  }
  .table-expand label {
    width: 100px;
    color: #99a9bf;
  }
  .table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
  }
  .goods-detail-box img {
    width: 100%;
  }
</style>

<script>
import { listProduct, deleteProduct } from '@/api/product'
import { simpleListFormula } from '@/api/formula'
import { getToken } from '@/utils/auth'
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Product',
  components: { BackToTop, Pagination },
  data() {
    return {
      time: [],
      list: [],
      total: 0,
      formula: [],
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined,
        sort: 'add_time',
        order: 'desc',
        startTime: undefined,
        endTime: undefined
      },
      formulaDetail: '',
      detailDialogVisible: false,
      downloadLoading: false,
      dataForm: {
        formulaId: undefined
      },
      dialogFormVisible: false
    }
  },
  computed: {
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  },
  created() {
    // this.initTime()
    this.getList()
    this.simpleListFormula()
  },
  methods: {
    getList() {
      this.listLoading = true
      listProduct(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSelectFormula() {
      this.dataForm = {
        formulaId: undefined
      }
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    openCreate() {
      this.$router.push({ path: '/stuff/product-create', query: { id: this.dataForm.formulaId }})
    },
    handleDelete(row) {
      deleteProduct(row)
        .then(response => {
          this.$notify.success({
            title: '成功',
            message: '删除成功'
          })
          this.getList()
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['产品ID', '产品名称', '总个数', '总价格', '简介']
        const filterVal = ['id', 'name', 'number', 'price', 'brief']
        excel.export_json_to_excel2(tHeader, this.list, filterVal, '产品信息')
        this.downloadLoading = false
      })
    },
    simpleListFormula() {
      simpleListFormula()
        .then(response => {
          this.formula = response.data.data
        })
        .catch(() => {
          this.formula = []
        })
    },
    initTime() {
      const end = new Date(new Date(new Date().toLocaleDateString()).getTime());
      const start = new Date(new Date(new Date().toLocaleDateString()).getTime()-3600 * 1000 *24);
      this.listQuery.endTime=end.getTime()
      this.listQuery.startTime=start.getTime()
      this.time=[start,end]
    },
    changeTime(time) {
      this.listQuery.startTime=time[0].getTime()
      this.listQuery.endTime=time[1].getTime()
      this.getList()
    }
  }
}
</script>
